home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEMP / GNU / flex / Matching < prev    next >
Text File  |  1995-06-28  |  4KB  |  84 lines

  1. Matching
  2. Previous: <Patterns=>Patterns> * Next: <Actions=>Actions> * Up: <Top=>!Root>
  3.  
  4. #Wrap on
  5. {fH3}How the input is matched{f}
  6.  
  7. When the generated scanner is run, it analyzes its input
  8. looking for strings which match any of its patterns.  If
  9. it finds more than one match, it takes the one matching
  10. the most text (for trailing context rules, this includes
  11. the length of the trailing part, even though it will then
  12. be returned to the input).  If it finds two or more
  13. matches of the same length, the rule listed first in the
  14. {fCode}flex{f} input file is chosen.
  15.  
  16. Once the match is determined, the text corresponding to
  17. the match (called the {fStrong}token{f}) is made available in the
  18. global character pointer {fCode}yytext{f}, and its length in the
  19. global integer {fCode}yyleng{f}.  The {fStrong}action{f} corresponding to the
  20. matched pattern is then executed (a more detailed
  21. description of actions follows), and then the remaining input is
  22. scanned for another match.
  23.  
  24. If no match is found, then the {fUnderline}default rule{f} is executed:
  25. the next character in the input is considered matched and
  26. copied to the standard output.  Thus, the simplest legal
  27. {fCode}flex{f} input is:
  28.  
  29. #Wrap off
  30. #fCode
  31. %%
  32. #f
  33. #Wrap on
  34.  
  35. which generates a scanner that simply copies its input
  36. (one character at a time) to its output.
  37.  
  38. Note that {fCode}yytext{f} can be defined in two different ways:
  39. either as a character {fEmphasis}pointer{f} or as a character {fEmphasis}array{f}.
  40. You can control which definition {fCode}flex{f} uses by including
  41. one of the special directives {fEmphasis}%pointer{f} or {fEmphasis}%array{f} in the
  42. first (definitions) section of your flex input.  The
  43. default is {fEmphasis}%pointer{f}, unless you use the {fEmphasis}-l{f} lex
  44. compatibility option, in which case {fCode}yytext{f} will be an array.  The
  45. advantage of using {fEmphasis}%pointer{f} is substantially faster
  46. scanning and no buffer overflow when matching very large
  47. tokens (unless you run out of dynamic memory).  The
  48. disadvantage is that you are restricted in how your actions can
  49. modify {fCode}yytext{f} (see the next section), and calls to the
  50. {fEmphasis}unput(){f} function destroys the present contents of {fCode}yytext{f},
  51. which can be a considerable porting headache when moving
  52. between different {fCode}lex{f} versions.
  53.  
  54. The advantage of {fEmphasis}%array{f} is that you can then modify {fCode}yytext{f}
  55. to your heart's content, and calls to {fEmphasis}unput(){f} do not
  56. destroy {fCode}yytext{f} (see below).  Furthermore, existing {fCode}lex{f}
  57. programs sometimes access {fCode}yytext{f} externally using
  58. declarations of the form:
  59. #Wrap off
  60. #fCode
  61. extern char yytext[];
  62. #f
  63. #Wrap on
  64. This definition is erroneous when used with {fEmphasis}%pointer{f}, but
  65. correct for {fEmphasis}%array{f}.
  66.  
  67. {fEmphasis}%array{f} defines {fCode}yytext{f} to be an array of {fCode}YYLMAX{f} characters,
  68. which defaults to a fairly large value.  You can change
  69. the size by simply \#define'ing {fCode}YYLMAX{f} to a different value
  70. in the first section of your {fCode}flex{f} input.  As mentioned
  71. above, with {fEmphasis}%pointer{f} yytext grows dynamically to
  72. accommodate large tokens.  While this means your {fEmphasis}%pointer{f} scanner
  73. can accommodate very large tokens (such as matching entire
  74. blocks of comments), bear in mind that each time the
  75. scanner must resize {fCode}yytext{f} it also must rescan the entire
  76. token from the beginning, so matching such tokens can
  77. prove slow.  {fCode}yytext{f} presently does {fEmphasis}not{f} dynamically grow if
  78. a call to {fEmphasis}unput(){f} results in too much text being pushed
  79. back; instead, a run-time error results.
  80.  
  81. Also note that you cannot use {fEmphasis}%array{f} with C++ scanner
  82. classes (the {fCode}c++{f} option; see below).
  83.  
  84.